package org.limited.memory;

import cn.hutool.core.io.LineHandler;
import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.limited.memory.utils.Constant;
import org.limited.memory.utils.MemoryInfoUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class MapStatisticalDuplication {

    public static void main(String[] args) {
        //输出jvm内存信息
        MemoryInfoUtil.memoryInfo();

        //map用于存储结果
        Map<String,Integer> wordCountMap = new HashMap<>();
        //读取文件 并统计出现次数
        FileReader fileReader = new FileReader(Constant.bigFilePath);
        //在极端的情况下 所有字符串都不重复 会导致OOM
        LineHandler handler = line -> wordCountMap.put(line,wordCountMap.getOrDefault(line,0) + 1);
        try {
            fileReader.readLines(handler);
        }catch (Throwable e){
            MemoryInfoUtil.memoryInfo();
            log.error("read lines error:{}",e.getMessage(),e);
            return;
        }


        List<Map.Entry<String, Integer>> top20 = wordCountMap.entrySet()
                .stream()
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                .limit(20)
                .collect(Collectors.toList());

        log.info("top 20:{}", JSONObject.toJSONString(top20));
    }


}
